package com.smartsaas.ugclivii.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.smartsaas.ugclivii.dto.ResultDTO;
import com.smartsaas.ugclivii.ui.UiContext;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.net.URI;

public class RequestUtils {
    public static String get(URI url){
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            return EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            }catch (Exception ex){
            }
        }
    }
    public static String fileUpload(URI uri,String key, File file){
        HttpPost post = new HttpPost(uri);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.setCharset(Consts.UTF_8);
        builder.setContentType(ContentType.MULTIPART_FORM_DATA);
        builder.addBinaryBody(key,file,ContentType.MULTIPART_FORM_DATA,file.getName());
        HttpEntity entity = builder.build();
        post.setEntity(entity);
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        try(CloseableHttpResponse response = httpClient.execute(post)) {
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                return EntityUtils.toString(httpEntity, Consts.UTF_8);
            }
            return null;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static String postJsonFun(URI uri, String json){
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse response=null;
        HttpPost post = new HttpPost(uri);
        try {
            StringEntity stringEntity = new StringEntity(json,"UTF-8");
            stringEntity.setContentType("application/json;charset=utf-8");
            post.setEntity(stringEntity);
            response = httpClient.execute(post);
            return EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            }catch (Exception ex){
            }
        }
    }

    public static <T> ResultDTO<T> postFile(URI uri, File zip){
        String res = fileUpload(uri, "file", zip);
        return JSON.parseObject(res,new TypeReference<ResultDTO<T>>(){});
    }

    public static <T> ResultDTO<T> postJson(URI uri, String json){
        String res = postJsonFun(uri,json);
        return JSON.parseObject(res,new TypeReference<ResultDTO<T>>(){});
    }
    public static <T> ResultDTO<T> doGet(URI url ,TypeReference<ResultDTO<T>> t){
        UiContext.getInstance().debug("【请求】:"+url.toString());
        String s = get(url);
        UiContext.getInstance().debug("【响应】:"+s);
        return JSON.parseObject(s,t);
    }
}
